package com.yzxb.erpsystem.controller;

import com.yzxb.erpsystem.entity.Product;
import com.yzxb.erpsystem.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/products")
public class ProductController {
    
    @Autowired
    private ProductService productService;
    
    /**
     * 创建商品
     */
    @PostMapping
    public ResponseEntity<Product> createProduct(@RequestBody Product product) {
        // 请求参数校验
        if (product == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            Product createdProduct = productService.createProduct(product);
            return ResponseEntity.status(HttpStatus.CREATED).body(createdProduct);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().header("Error-Message", e.getMessage()).build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 更新商品
     */
    @PutMapping("/{id}")
    public ResponseEntity<Product> updateProduct(@PathVariable Long id, @RequestBody Product product) {
        // 请求参数校验
        if (id == null || product == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            Product updatedProduct = productService.updateProduct(id, product);
            return ResponseEntity.ok(updatedProduct);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().header("Error-Message", e.getMessage()).build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 删除商品
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteProduct(@PathVariable Long id) {
        // 请求参数校验
        if (id == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            productService.deleteProduct(id);
            return ResponseEntity.noContent().build();
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().header("Error-Message", e.getMessage()).build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 根据ID查询商品
     */
    @GetMapping("/{id}")
    public ResponseEntity<Product> getProductById(@PathVariable Long id) {
        // 请求参数校验
        if (id == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            Optional<Product> product = productService.getProductById(id);
            return product.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 根据商品编码查询商品
     */
    @GetMapping("/code/{productCode}")
    public ResponseEntity<Product> getProductByProductCode(@PathVariable String productCode) {
        // 请求参数校验
        if (productCode == null || productCode.trim().isEmpty()) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            Optional<Product> product = productService.getProductByProductCode(productCode);
            return product.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 查询所有商品
     */
    @GetMapping
    public ResponseEntity<List<Product>> getAllProducts() {
        try {
            List<Product> products = productService.getAllProducts();
            return ResponseEntity.ok(products);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 根据状态查询商品列表
     */
    @GetMapping("/status/{status}")
    public ResponseEntity<List<Product>> getProductsByStatus(@PathVariable String status) {
        // 请求参数校验
        if (status == null || status.trim().isEmpty()) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            List<Product> products = productService.getProductsByStatus(status);
            return ResponseEntity.ok(products);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 根据分类ID查询商品列表
     */
    @GetMapping("/category/{categoryId}")
    public ResponseEntity<List<Product>> getProductsByCategoryId(@PathVariable Long categoryId) {
        // 请求参数校验
        if (categoryId == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            List<Product> products = productService.getProductsByCategoryId(categoryId);
            return ResponseEntity.ok(products);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 上架商品
     */
    @PutMapping("/{id}/activate")
    public ResponseEntity<Product> activateProduct(@PathVariable Long id) {
        // 请求参数校验
        if (id == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            Product activatedProduct = productService.activateProduct(id);
            return ResponseEntity.ok(activatedProduct);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().header("Error-Message", e.getMessage()).build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
    
    /**
     * 下架商品
     */
    @PutMapping("/{id}/deactivate")
    public ResponseEntity<Product> deactivateProduct(@PathVariable Long id) {
        // 请求参数校验
        if (id == null) {
            return ResponseEntity.badRequest().build();
        }
        
        try {
            Product deactivatedProduct = productService.deactivateProduct(id);
            return ResponseEntity.ok(deactivatedProduct);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().header("Error-Message", e.getMessage()).build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).header("Error-Message", e.getMessage()).build();
        }
    }
}